/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//expr_project.hpp

#ifndef MVL_EXPR_PROJECT_HPP
#define MVL_EXPR_PROJECT_HPP

#include "expr_order.hpp"
#include "type_traits.hpp"
#include "layout_cofactor.hpp"
#include "layout_range.hpp"

namespace mvl {

template<typename L, typename E>
class expr_project : public expr<expr_project<L, E> > 
{ 
public:
    typedef expr_project self_t;

    //make sure expr_t has the same order as L
    typedef expr_order<typename L::order, E>    expr_t;

    typedef typename meta::if_<
        typename meta::is_const<E>::type,
        typename expr_t::const_closure_t,
        typename expr_t::closure_t
    >::type expr_closure_t;

public:
    typedef L                               layout_t;
    typedef typename expr_t::value_type     value_type;
    typedef typename layout_t::order        order;
    typedef typename layout_t::Rows         Rows;
    typedef typename layout_t::Cols         Cols;
    typedef typename layout_t::Size         Size;

    typedef self_t closure_t;
    typedef const self_t const_closure_t;

public:
    expr_project(expr_t& e) : m_e(e) { }

    self_t& operator = (self_t const& rhs) {
        mat_mat_assign<self_t, self_t, fn_assign<value_type, value_type> >::apply(*this, rhs);
        return *this;
    }

    template<typename E2>
    self_t& operator = (expr<E2> const& rhs) {
        MVL_STATIC_CHECK((E2::Rows::value == Rows::value), "mat_size_mismatch");
        MVL_STATIC_CHECK((E2::Cols::value == Cols::value), "mat_size_mismatch");
        mat_mat_assign<self_t, E2, fn_assign<value_type, value_type> >::apply(*this, rhs());
        return *this;
    }

    //access
    template<typename Entry>
    value_type at() const {
        return m_e.template at<typename Entry::src>();
    }

    template<typename Entry>
    value_type& at() {
        return m_e.template at<typename Entry::src>();
    }

private:
    expr_closure_t m_e;
};

template<typename L, typename E>
struct project_traits
{
    typedef expr_project<L, const E> type;
};

template<typename L, typename E>
inline 
const typename project_traits<L, const E>::type 
project(expr<E> const& e) {
    typedef const typename project_traits<L, const E>::type expr_t;
    return expr_t(e());
}

template<typename L, typename E>
inline 
typename project_traits<L, E>::type 
project(expr<E>& e) {
    typedef typename project_traits<L, E>::type expr_t;
    return expr_t(e());
}

//matrix obtained by eliminationg row I and column J
template<int I, int J, typename E>
struct cofactor_mat_traits {
    typedef typename layout::layout_cofactor<
        typename E::layout_t,
        meta::int_<I>,
        meta::int_<J>
    >::type layout_t;
    typedef typename project_traits<layout_t, E>::type result_t;
};

template<int I, int J, typename E>
inline 
const typename cofactor_mat_traits<I, J, const E>::result_t
cofactor_mat(expr<E> const& e)
{
    typedef const typename cofactor_mat_traits<I, J, const E>::result_t expr_t;
    return expr_t(e());
}

template<int I, int J, typename E>
inline 
typename cofactor_mat_traits<I, J, E>::result_t
cofactor_mat(expr<E>& e)
{
    typedef typename cofactor_mat_traits<I, J, E>::result_t expr_t;
    return expr_t(e());
}

//range matrix
template<int I1, int I2, int J1, int J2, typename E>
struct range_mat_traits {
    typedef typename layout::layout_range<
        typename E::layout_t,
        meta::int_<I1>,
        meta::int_<I2>,
        meta::int_<J1>,
        meta::int_<J2>
    >::type layout_t;
    typedef expr_project<layout_t, E> result_t;
};

template<int I1, int I2, int J1, int J2, typename E>
inline 
const typename range_mat_traits<I1, I2, J1, J2, const E>::result_t
range_mat(expr<E> const& e)
{
    typedef const typename range_mat_traits<I1, I2, J1, J2, const E>::result_t expr_t;
    return expr_t(e());
}

template<int I1, int I2, int J1, int J2, typename E>
inline 
typename range_mat_traits<I1, I2, J1, J2, E>::result_t
range_mat(expr<E>& e)
{
    typedef typename range_mat_traits<I1, I2, J1, J2, E>::result_t expr_t;
    return expr_t(e());
}

template<int I, typename E>
struct row_mat_traits : range_mat_traits<I, I + 1, 0, E::Cols::value, const E> {};

//row 
template<int I, typename E>
inline 
const typename row_mat_traits<I, const E>::result_t
row_mat(expr<E> const &e) 
{
    typedef const typename row_mat_traits<I, const E>::result_t expr_t;
    return expr_t(e());
}

template<int I, typename E>
inline 
typename row_mat_traits<I, E>::result_t
row_mat(expr<E> &e) 
{
    typedef typename row_mat_traits<I, E>::result_t expr_t;
    return expr_t(e());
}

template<int J, typename E>
struct column_mat_traits : range_mat_traits<0, E::Rows::value, J, J + 1, const E> {};

//column 
template<int J, typename E>
inline 
const typename column_mat_traits<J, const E>::result_t
column_mat(expr<E> const &e) 
{
    typedef const typename column_mat_traits<J, const E>::result_t expr_t;
    return expr_t(e());
}

template<int J, typename E>
inline 
typename column_mat_traits<J, E>::result_t
column_mat(expr<E> &e) 
{
    typedef typename column_mat_traits<J, E>::result_t expr_t;
    return expr_t(e());
}

} //namespace mvl

#endif
